package com.shr25.robot.qq.plugins;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shr25.robot.common.RobotMsgPermission;
import com.shr25.robot.common.RobotMsgType;
import com.shr25.robot.qq.model.QqMessage;
import com.shr25.robot.qq.util.MessageUtil;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.User;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.message.data.*;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 树人助人分享信息
 *
 * @author huobing
 * @date 2022/6/8 16:44
 */
@Component
public class TaskShr25Plugin extends RobotPlugin {
    public TaskShr25Plugin() {
        super();
        setName("树人分享插件");
        addDesc("查找树人分享的信息");
        addCommand("树人分享","查看所有树人分享的网赚，慢慢更新中,需要@", RobotMsgPermission.MEMBER, new RobotMsgType[]{RobotMsgType.Group}, qqMessage -> {
            JSONObject data = shr25(qqMessage.getGroupId(), qqMessage.getCommand());

            if(data != null){
                MessageChainBuilder builder = MessageUtil.createBuilder();
                builder.append(new PlainText(data.getStr("title")+ "\n" +data.getStr("jurl")+ "\n" +data.getStr("desc")));
                qqMessage.putReplyMessage(builder);
                qqMessage.putReplyMessage(new LightApp("{\"app\":\"com.tencent.structmsg\",\"config\":{\"ctime\":1659065014,\"forward\":true,\"token\":\"8897abe7d1faf0fe16677185c5f38f33\",\"type\":\"normal\"},\"desc\":\"新闻\",\"meta\":{\"news\":{\"action\":\"\",\"android_pkg_name\":\"\",\"app_type\":1,\"appid\":100446242,\"ctime\":1659065014,\"desc\":\"更多网赚平台@QQ浏览器\",\"jumpUrl\":\"https://task.shr25.com/app/morePlatform\",\"preview\":\"https://pic.ugcimg.cn/a412e55cb41ebc2500e11a8952f7f13f/jpg1\",\"source_icon\":\"http://p.qpic.cn/qqconnect_app_logo/PdibpV1sFDHf2jwsEP888ibhf9XK05hdWvQUxdQMk7uVE/0\",\"source_url\":\"\",\"tag\":\"QQ浏览器\",\"title\":\"更多网赚平台\",\"uin\":344420550}},\"prompt\":\"[分享]更多网赚平台\",\"ver\":\"0.0.0.1\",\"view\":\"news\"}"));
            }
            return true;
        }, true);
        setSort(1);
    }

    public boolean executeMessage(QqMessage qqMessage) {
        AtomicBoolean executeNext = new AtomicBoolean(true);
        // 消息为空、好友消息、群里被@
        if (StringUtils.isNotBlank(qqMessage.getCommand())) {

        }
        return executeNext.get();
    }

    public JSONObject shr25(Long groupId, String msg) {
        String url = "https://task.shr25.com/robot/get";
        try {
            Map<String, Object> data = new HashMap<>();
            data.put("groupId", groupId);
            data.put("msg", msg);
            data.put("version", "2");
            return JSONUtil.parseObj(HttpUtil.post(url, data)).get("data", JSONObject.class);
        } catch (Exception e) {
            log.error("请求树人分享系统失败！", e);
            return null;
        }
    }
    public static MessageChain buildShareCardMessage(JSONObject data, User user) {
        return buildShareCardMessage(data.getStr("jurl"),data.getStr("title"),data.getStr("desc"),data.getStr("coverUrl"), user);
    }

    /**
     * 分享卡片消息
     * @param jurl       分享地址
     * @param title     标题
     * @param desc   描述
     * @param coverUrl  封面
     * @return
     */
    public static MessageChain buildShareCardMessage(String jurl, String title, String desc, String coverUrl, User user) {
        //QQ浏览器标识
        Integer appid = 100446242;
        String  tag = "QQ浏览器";
        String  sourceIcon = "http://p.qpic.cn/qqconnect_app_logo/PdibpV1sFDHf2jwsEP888ibhf9XK05hdWvQUxdQMk7uVE/0";

        Long ctime = System.currentTimeMillis()/1000;
        JSONObject appmsg = new JSONObject(true);
        appmsg.putOnce("app", "com.tencent.structmsg");
        JSONObject config = new JSONObject(true);
        config.putOnce("ctime", ctime);
        config.putOnce("forward", true);
        config.putOnce("token", "fd635215546f1ecada3aaed146c43c00");
        config.putOnce("type", "normal");
        appmsg.putOnce("config", config);
        appmsg.putOnce("desc", "新闻");
        JSONObject meta = new JSONObject(true);
        appmsg.putOnce("meta", meta);
        JSONObject news = new JSONObject(true);
        meta.putOnce("news", news);
        news.putOnce("action", "");
        news.putOnce("android_pkg_name", "");
        news.putOnce("app_type", 1);
        news.putOnce("appid", appid);
        news.putOnce("ctime", ctime);
        news.putOnce("desc", desc);
        news.putOnce("jumpUrl", jurl.replaceAll("/","\\\\/"));
        news.putOnce("preview", coverUrl.replaceAll("/","\\\\/"));
        news.putOnce("source_icon", sourceIcon.replaceAll("/","\\\\/"));
        news.putOnce("source_url", "");
        news.putOnce("tag", tag);
        news.putOnce("title", title);
        news.putOnce("uin", user.getId());
        appmsg.putOnce("prompt", "[分享]"+title);
        appmsg.putOnce("ver", "0.0.0.1");
        appmsg.putOnce("view", "news");
        return new LightApp(appmsg.toString()).plus("");
    }
}
